package com.sinoglobal.ningxia.fragment;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.mico.PlayBillActivity;
import com.sinoglobal.ningxia.adapter.PlayBillAdapter;
import com.sinoglobal.ningxia.beans.BillVo;
import com.sinoglobal.ningxia.beans.PlayBillBean;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.thread.ItktAsyncTaskWithDialog;
import com.sinoglobal.ningxia.utils.ItktLog;

@SuppressLint("ValidFragment")
public class PlayBillFragment extends Fragment {

	private View view;// 频道view
	private ListView playBill;
	PlayBillAdapter adapter;
	// PlayBillTask task;
	ItktAsyncTaskWithDialog<Integer, Void, BillVo> task;
	private int channelId ;
	TextView noData;
	ProgressBar mProgressBar;
	public static List<PlayBillBean> list = new ArrayList<PlayBillBean>();

	public PlayBillFragment(int channelId) {
		this.channelId = channelId;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		view = LayoutInflater.from(getActivity()).inflate(R.layout.play_bill_fragment, null);
		init();
		getData();
		return view;
	}

	private void init() {
		noData = (TextView) view.findViewById(R.id.noData);
		mProgressBar = (ProgressBar) view.findViewById(R.id.playbillProgress);
		playBill = (ListView) view.findViewById(R.id.playBillListView);
		playBill.setDivider(null);
		playBill.setDividerHeight(0);

	}

	public void getData() {
		List<PlayBillBean> data = PlayBillActivity.mapData.get(channelId);
		if (data != null && data.size() > 0) {
			adapter = new PlayBillAdapter(getActivity(), data, true);
			playBill.setAdapter(adapter);
			return;
		}
		task = new ItktAsyncTaskWithDialog<Integer, Void, BillVo>(getString(R.string.loading), true, true) {

			@Override
			public void exception() {
				mProgressBar.setVisibility(View.GONE);
				noData.setVisibility(View.VISIBLE);
			}

			@Override
			public BillVo before(Integer... params) throws Exception {
				BillVo vo = null;

				try {
					vo = RemoteImpl.getInstance().getPlayBill(params[0]);
				} catch (Exception e) {
					e.printStackTrace();
				}
				Log.e("result", params[0]+"---"+vo.getCode());
				return vo;
			}

			@Override
			public void after(BillVo result) {
				if (mProgressBar != null)
					mProgressBar.setVisibility(View.VISIBLE);
				Log.e("result", "---"+result.getCode());
				if (result != null && Code.CODE_SUCCESS == result.getCode()) {
					Log.e("size", "---"+result.getList().size());
					if (result.getList().size() > 0) {
						if (getActivity() == null)
							return;
						adapter = new PlayBillAdapter(getActivity(), result.getList(), true);
						playBill.setAdapter(adapter);
						PlayBillActivity.mapData.put(channelId, result.getList());
						noData.setVisibility(View.GONE);
					} else {
						noData.setVisibility(View.VISIBLE);
					}

				} else {

					noData.setVisibility(View.VISIBLE);
				}
				mProgressBar.setVisibility(View.GONE);

			}

		};
		task.execute(channelId);

		/*
		 * task = new PlayBillTask() {
		 * 
		 * @Override public void after(BillVo vo) { if (Code.CODE_SUCCESS ==
		 * vo.getCode()) { adapter = new PlayBillAdapter(getActivity(),
		 * vo.getList(), true); playBill.setAdapter(adapter);
		 * noData.setVisibility(View.GONE);
		 * 
		 * } else { noData.setVisibility(View.VISIBLE); } } };
		 * task.execute(channelId);
		 */

	}

	/**
	 * @author qiwx
	 * @createdate 2014-10-13 上午10:25:45
	 * @description
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		if (task != null && task.getStatus() == AsyncTask.Status.RUNNING) {
			task.cancel(true);// 如果Task还在运行，则先取消它
			task = null;
		}
	}

}
